package com.hellobike.haxing.modules.pms.purchaseReturn.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.annotation.AutoLog;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.system.vo.LoginUser;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.pms.enums.PurchaseReturnStatusEnum;
import com.hellobike.haxing.modules.pms.purchaseReturn.entity.ReturnSupplierDetailVo;
import com.hellobike.haxing.modules.pms.purchaseReturn.entity.ReturnSupplierHeaderVo;
import com.hellobike.haxing.modules.product.entity.Sku;
import com.hellobike.haxing.modules.product.entity.Spu;
import com.hellobike.haxing.modules.product.service.ISkuService;
import com.hellobike.haxing.modules.product.service.ISpuService;
import com.hellobike.haxing.modules.supplier.entity.TSupplierInfo;
import com.hellobike.haxing.modules.supplier.service.ITSupplierInfoService;
import com.hellobike.haxing.modules.supplier.util.BeanUtils;
import com.hellobike.haxing.modules.wms.controller.WmsBaseController;
import com.hellobike.haxing.modules.wms.entity.ReturnSupplierDetail;
import com.hellobike.haxing.modules.wms.entity.ReturnSupplierHeader;
import com.hellobike.haxing.modules.wms.entity.ReturnWarehouseHeader;
import com.hellobike.haxing.modules.wms.service.IReturnSupplierDetailService;
import com.hellobike.haxing.modules.wms.service.IReturnSupplierHeaderService;
import com.hellobike.haxing.modules.wms.service.IReturnWarehouseHeaderService;
import com.hellobike.haxing.modules.wms.utils.ListUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
* @Author: haxing-boot
* @Date:   2020-02-07
* @Version: V1.0
*/
@RestController
@RequestMapping("/purchaseReturn/returnWarehouseHeader")
@Slf4j
public class PurchaseReturnHeaderController extends WmsBaseController<ReturnWarehouseHeader, IReturnWarehouseHeaderService> {
   @Autowired
   private IReturnSupplierHeaderService returnSupplierHeaderService;
	@Autowired
	private IReturnSupplierDetailService returnSupplierDetailService;
	@Autowired
	private ISkuService skuService;
	@Autowired
	private ISpuService spuService;
	@Autowired
	private ITSupplierInfoService tSupplierInfoService;

	/**
	 * 分页列表查询
	 *
	 * @param returnSupplierHeader
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "退货管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ReturnSupplierHeader returnSupplierHeader,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		returnSupplierHeader.setWarehouseId(getWarehouseId());
		QueryWrapper<ReturnSupplierHeader> queryWrapper = QueryGenerator.initQueryWrapper(returnSupplierHeader, req.getParameterMap());
		Long supplierId = getSupplierId();
		if(supplierId == null){
			return Result.error("用户不存在");
		}
		queryWrapper.eq("supplier_id", supplierId);
		Page<ReturnSupplierHeader> page = new Page<ReturnSupplierHeader>(pageNo, pageSize);
		IPage<ReturnSupplierHeader> pageList = returnSupplierHeaderService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	private Long getSupplierId(){
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		if(sysUser == null){
			return null;
		}
		QueryWrapper<TSupplierInfo> tSupplierInfoQueryWrapper = new QueryWrapper<>();
		tSupplierInfoQueryWrapper.eq("mobile", sysUser.getPhone());
		TSupplierInfo tSupplierInfo = tSupplierInfoService.getOne(tSupplierInfoQueryWrapper);
		if(tSupplierInfo == null){
			return null;
		}
		return tSupplierInfo.getId();
	}


	/**
	 * 通过id查询
	 *
	 * @param json
	 * @return
	 */
	@PostMapping(value = "/detail")
	public Result<?> detail(@RequestBody JSONObject json) {
		Long id = json.getLong("headerId");
		ReturnSupplierHeader returnSupplierHeader = returnSupplierHeaderService.getById(id);
		if(returnSupplierHeader == null) {
			return Result.error("未找到对应数据");
		}
		ReturnSupplierHeaderVo returnSupplierHeaderVo = new ReturnSupplierHeaderVo();
		BeanUtils.copyProperties(returnSupplierHeader, returnSupplierHeaderVo);
		returnSupplierHeaderVo.setReturnStatusStr(PurchaseReturnStatusEnum.getName(returnSupplierHeaderVo.getReturnStatus()));

		ReturnSupplierDetail param = new ReturnSupplierDetail();
		param.setReturnHeaderId(id);
		Wrapper<ReturnSupplierDetail> wrapper = new QueryWrapper<>(param);
		List<ReturnSupplierDetail> returnSupplierDetailList = returnSupplierDetailService.list(wrapper);
		List<ReturnSupplierDetailVo> returnSupplierDetailVos = new ArrayList<>();
		if(ListUtil.isNotEmpty(returnSupplierDetailList)){
			returnSupplierDetailList.forEach(
					item -> {
						ReturnSupplierDetailVo returnSupplierDetailVo = new ReturnSupplierDetailVo();
						Sku sku = skuService.getById(item.getSkuId());
						Spu spu = spuService.getById(sku.getSpuId());
						BeanUtils.copyProperties(item, returnSupplierDetailVo);
						returnSupplierDetailVo.setSkuName(spu.getSpuName());
						returnSupplierDetailVos.add(returnSupplierDetailVo);
					}
			);
			returnSupplierHeaderVo.setReturnSupplierDetailVos(returnSupplierDetailVos);
		}
		return Result.ok(returnSupplierHeaderVo);
	}

	/**
	 *  编辑
	 *
	 * @param returnSupplierHeader
	 * @return
	 */
	@AutoLog(value = "退货管理-编辑状态",operateType=3)
	@PutMapping(value = "/editStatus")
	public Result<?> editStatus(@RequestBody ReturnSupplierHeader returnSupplierHeader) {
		returnSupplierHeaderService.updateById(returnSupplierHeader);
		return Result.ok("编辑成功!");
	}

}
